﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using ICSharpCode.AvalonEdit.Rendering;
using ICSharpCode.AvalonEdit;

namespace LogoScriptIDE
{
    /// <summary>
    /// 当前行高亮器
    /// </summary>
    class CaretLineRenderer : IBackgroundRenderer
    {
        private TextEditor m_editor;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="editor">与之关联的编辑器</param>
        public CaretLineRenderer(TextEditor editor)
        {
            m_editor = editor;
        }

        /// <summary>
        /// 获得该高亮器所在的层（固定为光标层）
        /// </summary>
        public KnownLayer Layer
        {
            get { return KnownLayer.Caret; }
        }

        /// <summary>
        /// 绘制回调函数
        /// </summary>
        public void Draw(TextView textView, DrawingContext drawingContext)
        {
            if (m_editor.Document == null)
                return;

            textView.EnsureVisualLines();
            // 获得当前行
            var currentLine = m_editor.Document.GetLineByOffset(m_editor.CaretOffset);
            
            // 绘制背景
            foreach (var rect in BackgroundGeometryBuilder.GetRectsForSegment(textView, currentLine))
            {
                double drawingWidth = textView.ScrollOffset.X + textView.ActualWidth;
                drawingContext.DrawRectangle(
                    new SolidColorBrush(BgColor), null,
                    new Rect(rect.Location, new Size(drawingWidth, rect.Height)));
            }
        }

        private Color m_color = Color.FromArgb(0x20, 0, 0, 0xFF);

        /// <summary>
        /// 获取或设置当前行高亮颜色
        /// </summary>
        public Color BgColor {
            get { return m_color; }
            set
            {
                m_color = value;
                // 重绘
                m_editor.TextArea.TextView.InvalidateLayer(Layer);
            }
        }
    }
}
